﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Sina.Altman.Weibo.Client.Commons;
using Sina.Altman.Weibo.Languages;
using Microsoft.Phone.Tasks;
using System.IO;
using Sina.Altman.Weibo.SDK.Objects;
using Sina.Altman.Weibo.Tools;
using Sina.Altman.Weibo.SDK;

namespace Sina.Altman.Weibo.Client.View
{
    public partial class SendWeiboPage : PhoneApplicationPage
    {
        PhotoChooserTask photoChooserTask;

        public SendWeiboPage()
        {
            InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
            Loaded += new RoutedEventHandler(SendWeiboPage_Loaded);
        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                if (e.ChosenPhoto.Length > 5 * 1021 * 1024)//> 5MB
                {

                }
                else
                {
                    var sentContent = new WeiboContent();
                    sentContent.Status = this.weiboTB.Text;
                    sentContent.Files=new Files();
                    sentContent.Files.Add("pic",new UploadFile(e.OriginalFileName,e.ChosenPhoto));
                    List<Parameter> parameters = new List<Parameter>();
                    parameters.Add(new Parameter("status", sentContent.Status));
                    parameters.Add(new Parameter("access_token", ISHelper.LoadSetting<string>(Constants.User_Access_Token_SR, string.Empty)));
                    sentContent.Parameters = parameters;

                    StatusesRequest.GetRequest(StateType.Upload, new StatusesRequestData() { WeiboContent=sentContent}, (result) =>
                        {
                        });
                }
            }
        }

        void SendWeiboPage_Loaded(object sender, RoutedEventArgs e)
        {
            InitialAppBar();
        }

        private ApplicationBarIconButton _sendWeiboButton;
        private ApplicationBarIconButton _memtionButton;
        private ApplicationBarMenuItem _picMenuItem;

        private void InitialAppBar()
        {
            if (base.ApplicationBar == null)
            {
                base.ApplicationBar = new ApplicationBar();
                base.ApplicationBar.Buttons.Clear();
                base.ApplicationBar.MenuItems.Clear();

                this._sendWeiboButton = new ApplicationBarIconButton(new Uri(ResourceConstants.SendWeiboPic, UriKind.Relative));
                this._sendWeiboButton.Text = LangResource.SendWeibo;
                this._sendWeiboButton.Click += new EventHandler((s, e) =>
                {
                    WeiboContent content = new WeiboContent() { Status=weiboTB.Text};
                    StatusesRequest.GetRequest(StateType.Update, new StatusesRequestData() { WeiboContent=content }, (result) =>
                        {

                        });
                });

                this._memtionButton = new ApplicationBarIconButton(new Uri(ResourceConstants.RefreshPic, UriKind.Relative));
                this._memtionButton.Text = LangResource.Refresh;
                this._memtionButton.Click += new EventHandler((s, e) =>
                {

                });

                this._picMenuItem = new ApplicationBarMenuItem(LangResource.InsertPic);
                this._picMenuItem.Click += new EventHandler((s, e) =>
                {
                    photoChooserTask.Show();
                });

                base.ApplicationBar.Buttons.Add(_sendWeiboButton);
                base.ApplicationBar.Buttons.Add(_memtionButton);
                base.ApplicationBar.MenuItems.Add(_picMenuItem);
            }
        }
        
    }
}